<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\yperp\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\yperp\common\Changhong;
use app\yperp\common\ChanghongList;
use app\yperp\common\ExcelTools;
use app\yperp\common\Redis;
use app\yperp\model\Brand;
use app\yperp\model\Supplier as SupplierModel;
use think\Db;

/**
 * 商品完善操作控制器
 * @package app\yperp\admin
 */
class Perfect extends Admin
{
    /**
     * 供应商列表
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {

        // 查询
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder('update_time desc');
        // 数据列表
        $data_list = SupplierModel::where($map)
            ->where('status > 0')
            ->order($order)->paginate();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setSearch(['corp_name' => '公司名称', 'goods_code' => '资料编码', 'goods_title' => '商品名称', 'goods_title_alias' => '商品名称别名', 'goods_sku' => '商品编码', 'goods_sku_alias' => '商品编码别名']) // 设置搜索框
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                // ['corp_name', '公司名称', 'text'],
                ['right_button', '操作', 'btn'],

                ['status', '状态', 'status', '', SupplierModel::STATUS],
                ['brand_id', '品牌',   Brand::column('name', 'id')],
                ['goods_color_id', '商品规格图', 'callback', 'goods_color_image', '__data__'],
                ['goods_main_image', '商品主图', 'picture'],
                ['goods_main_image_temp', '临时图', 'picture'],

                // ['goods_main_image', '合并logo', 'callback', function ($value, $data) {
                //     return '<a href="' . admin_url('yperp/barand/conflate', ['goods_id' => $data['id']]) . '" target="_blank" title="点击查看大图"><img src="' . $value . '" style="max-width: 100px;" /></a>';
                // }, '__DATA__'],
                ['goods_code', '资料编码', 'text'],
                ['goods_title', '商品名称', 'text'],
                ['goods_title_alias', '商品名称', 'text.edit'],
                ['goods_color', '颜色', 'text'],
                ['goods_color_alias', '颜色', 'text.edit'],
                // ['goods_color_id', '颜色id', 'text'],
                ['goods_specs', '规格', 'text'],
                ['goods_sku', '商品编码', 'text'],
                ['goods_sku_alias', '商品编码', 'text.edit'],
                // ['goods_in_price', '供货价', 'text'],
                // ['goods_buy_price', '零售价', 'text'],
                // ['goods_retail_price', '分销控价', 'text'],
                // ['goods_buy_number', '库存', 'text'],
                // ['goods_carriage', '运费', 'text.edit'],
                // ['goods_fluctuation', '溢价率', 'text.edit'],

                // ['goods_estimate_price', '预估价', 'callback', function ($data) {
                //     return ($data['goods_carriage'] + $data['goods_in_price']) * $data['goods_fluctuation'];
                // }, '__data__'],

                // ['sell_market_price', '市场价', 'text.edit'],
                // ['sell_friends_price', '友商价', 'text.edit'],
                // ['sell_daily_price', '日销价', 'text.edit'],
                // ['sell_activity_price', '活动价', 'text.edit'],
                // ['sell_coupon_price', '红码价', 'text.edit'],
                // ['sell_brokerage_price', '佣金', 'text.edit'],
                // ['other', '其他', 'text.edit'],
                ['goods_code_alias', '资料编码地址', 'callback', function ($value) {
                    return '<a date-copy>' . env('root_path') . 'public\uploads\goods\\' . $value . '</a>';
                }],
                // ['create_time', '创建时间', 'datetime'],
                // ['update_time', '更新时间', 'datetime'],
                ['status', '状态', 'status', '',  SupplierModel::STATUS],
                ['right_button', '操作', 'btn'],
            ])
            ->setTableName('yperp_supplier')
            ->addTopButtons('enable,disable') // 批量添加顶部按钮
            ->addRightButton('custom', [
                'title' => '推送',
                'icon'  => 'fa fa-fw fa-upload',
                'class' => 'btn btn-success ajax-get',
                'href'  => url('push', ['type' => 'push', 'id' => '__id__'])
            ])
            ->addRightButton('custom', [
                'title' => '推送更新',
                'icon'  => 'fa fa-fw fa-upload',
                'class' => 'btn btn-success ajax-get',
                'href'  => url('push', ['type' => 'update', 'id' => '__id__'])
            ])
            ->addRightButton('custom', [
                'title' => '合并logo',
                'icon'  => 'fa fa-fw fa-upload',
                'class' => 'btn btn-success ajax-get',
                'href'  =>  url('/yperp/brand/conflate', ['goods_id' => '__id__'])
            ])
            ->addRightButton('edit')
            ->addRightButton('view', [
                'title' => '预览',
                'icon'  => 'fa fa-fw fa-magic',
                'class' => 'btn btn-success',
                'href'  => url('supplier/view', ['goods_id' => '__id__'])
            ], ['area' => ['600px', '90%']])
            ->addTopButton('push', [
                'title' => '推送',
                'icon'  => 'fa fa-fw fa-upload',
                'field' => 'id',
                'target-form' => 'ids',
                'class' => 'btn btn-success ajax-post confirm',
                'href'  => url('push', ['type' => 'push'])
            ])

            ->addTopButton('push', [
                'title' => '更新',
                'icon'  => 'fa fa-fw fa-upload',
                'field' => 'id',
                'target-form' => 'ids',
                'class' => 'btn btn-success ajax-post confirm',
                'href'  => url('push', ['type' => 'update'])
            ])
            ->addTopButton('push', [
                'title' => '推送头图',
                'icon'  => 'fa fa-fw fa-upload',
                'field' => 'id',
                'target-form' => 'ids',
                'class' => 'btn btn-success ajax-post confirm',
                'href'  => url('push', ['type' => 'image'])
            ])
            ->addTopButton('push', [
                'title' => '检查',
                'icon'  => 'fa fa-fw fa-upload',
                'field' => 'id',
                'target-form' => 'ids',
                'class' => 'btn btn-success ajax-post confirm',
                'href'  => url('push', ['type' => 'check'])
            ])
            ->addTopButton('push', [
                'title' => '设计器',
                'icon'  => 'fa fa-fw fa-magic',
                // 'field' => 'id',
                // 'target-form' => 'ids',
                'class' => 'btn btn-success',
                'href'  => module_config('yperp.fastposter_url')
            ], true)

            ->addOrder('id,create_time,update_time')
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染模板
    }
    public function push()
    {
        $ids = $this->request->param('ids', false);
        $id = $this->request->param('id', false);
        $type =   $this->request->param('type', 'push');
        if ($id || $ids) {
            if ($id) {
                $ids = [$id];
            }
        } else {
            $this->error('参数错误');
        }
        $res = [];
        switch ($type) {
            case 'check':
                $res =   SupplierModel::checkStatus($ids);
                break;

            case 'update':
                $res =   SupplierModel::pushUpdate($ids);
                break;

            case 'image':
                $res =   SupplierModel::pushImage($ids);
                break;

            default:
                $res =   SupplierModel::push($ids);
                break;
        }


        if ($res) {
            $this->success('推送成功');
        } else {
            $this->error('推送失败');
        }
    }


    /**
     * 编辑
     * @param null $id 链接id
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $data['goods_main_image_temp'] = $data['goods_main_image'];
            // 验证
            if (SupplierModel::update($data)) {
                // 记录行为
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        }

        $info = SupplierModel::get($id);

        // 显示编辑页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['select', 'brand_id', '品牌logo', '', Brand::column('name', 'id')],
                ['text', 'goods_code', '资料编码'],
                ['text', 'goods_title_alias', '商品名称'],
                ['text', 'goods_color_alias', '颜色'],
                ['text', 'goods_color_id', '颜色id'],
                ['text', 'goods_specs', '规格'],
                ['text', 'goods_sku_alias', '商品编码'],
                ['text', 'goods_in_price', '供货价'],
                ['text', 'goods_buy_price', '零售价'],
                ['text', 'goods_retail_price', '分销控价'],
                ['text', 'goods_buy_number', '库存'],
                ['image', 'goods_main_image', '商品主图'],

                ['radio', 'status', '立即启用', '', ['否', '是']]
            ])
            ->setFormData($info)
            ->fetch();
    }
}
